package com.mobisys.live.controller.pc_web;

import com.mobisys.live.dao.model.Video;
import com.mobisys.live.response.Response;
import com.mobisys.live.service.VideoService;
import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.mobisys.live.config.ResponseCode.*;

/**
 * Created by liuchuan
 * 视频网站的video接口
 */
@RestController
public class VideoWebController {

    @Autowired
    private VideoService videoService;
    private Logger logger = LogManager.getLogger(VideoWebController.class.getName());
    /*
    * 获取所有点播视频
    *
    * */
    @RequestMapping(value = "/home/getVideoList",method = RequestMethod.GET)
    public Response getVideoList(String type,String currPage){

        Response response=new Response();
        List<Video> list = new ArrayList<>();
        JSONObject listobject = new JSONObject();
        int cur = Integer.valueOf(currPage);
        int pageSize = 20;
        //video获取
        list = videoService.findAll(cur,pageSize,type);

          if(!(list.isEmpty())){
              response.setRetureValue(SUCCESS);
              response.setRetureMsg(MSG_SUCCESS);
              listobject.put("listobect",list);
              response.setRetureData(listobject);
              return response;
          }
        response.setRetureValue(VIDEO_TYPE_FINDALL_ERROR);
        response.setRetureMsg(MSG_VIDEO_TYPE_FINDALL_ERROR);
        logger.debug("video查找为空");
        listobject.put("listobect",list);
        response.setRetureData(listobject);
        return response;


        }
        /*
        * 获取单个视频的相关信息
        *
        * */
    @RequestMapping(value = "/video/play",method = RequestMethod.GET)
    public Response getVideo(String id){
        Response response=new Response();
        JSONObject videoinfo = new JSONObject();
        Video video = videoService.selectByPrimaryKey(id);
        if(video == null){
            logger.debug("视频查找失败");
            response.setRetureValue(VIDEO_TYPE_FINDALL_ERROR);
            response.setRetureMsg(MSG_VIDEO_TYPE_FINDALL_ERROR);
        }else {
            response.setRetureValue(SUCCESS);
            response.setRetureMsg(MSG_SUCCESS);
        }
        videoinfo.put("videoinfo",video);
        response.setRetureData(videoinfo);

        return response;
    }



}
